package com.example.Gui;

import com.example.AdminComponent.Course;
import com.example.AdminComponent.Student;
import com.example.AdminComponent.Teacher;
import com.example.AdminComponent.PassJFrame;
import com.example.StudentComponent.Score;
import com.example.StudentComponent.SelectCourse;
import com.example.StudentComponent.StudentCourse;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StudentUi {
    JFrame jFrame=new JFrame("欢迎进入湖南科技大学综合服务平台");
    private String user;
    private String passWd;


    void init(String user,String passWd){
        this.user=user;
        this.passWd=passWd;
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
        jFrame.setSize(900,500);
        jFrame.setLocationRelativeTo(null);
        jFrame.setResizable(false);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //设置菜单栏
        JMenuBar jmb=new JMenuBar();  //菜单条
        JMenu jMenu=new JMenu("设置");
        JMenuItem m1=new JMenuItem("切换账号");
        JMenuItem m2=new JMenuItem("退出程序");
        JMenuItem m3=new JMenuItem("修改密码");
        //点击切换账号
        m1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new LoginUI().init();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                jFrame.dispose();
            }
        });

        //点击退出程序
        m2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        //点击修改密码
        m3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new PassJFrame(user,passWd);
            }
        });
        jMenu.add(m1);
        jMenu.add(m2);
        jMenu.add(m3);
        jmb.add(jMenu);
        jFrame.setJMenuBar(jmb);

        //设置分割面板
        JSplitPane jSplitPane=new JSplitPane();
        //支持连续布局
        jSplitPane.setContinuousLayout(true);
        jSplitPane.setDividerLocation(150);
        jSplitPane.setDividerSize(5);
        //设置左侧内容
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("学生服务");
        DefaultMutableTreeNode score = new DefaultMutableTreeNode("已选课程");
        DefaultMutableTreeNode selectClass = new DefaultMutableTreeNode("选课系统");
        DefaultMutableTreeNode course = new DefaultMutableTreeNode("课程表");
        root.add(score);
        root.add(selectClass);
        root.add(course);
        JTree tree=new JTree(root);
        jSplitPane.setLeftComponent(tree);
        //监听student
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                Object lastPathComponent = e.getNewLeadSelectionPath().getLastPathComponent();
                if(lastPathComponent.equals(score)){
                    //默认左右宽度
                    jSplitPane.setDividerLocation(150);
                    jSplitPane.setRightComponent(new Score(user));

                } else if (lastPathComponent.equals(selectClass)) {
                    jSplitPane.setDividerLocation(150);
                    jSplitPane.setRightComponent(new SelectCourse(user));
                }else if (lastPathComponent.equals(course)) {
                    jSplitPane.setDividerLocation(150);
                    jSplitPane.setRightComponent(new StudentCourse(user));
                }
            }
        });

        //设置tree默认选中查看成绩
        tree.setSelectionRow(1);







        jFrame.add(jSplitPane);

    }
}
